package checkReg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import util.Utils;

public class CheckRssServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			URL url = new URL(req.getParameter("name"));

			String charset = "UTF-8";
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setInstanceFollowRedirects(true);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), Charset.forName(charset)));
			String content = "";
			while (true) {
				String str = reader.readLine();
				if (str == null)
					break;
				content = content + str;
			}
			Document doc = Jsoup.parse(content);
			String link = doc.select("item").eq(0).select("comments").eq(0)
					.text().replaceAll("#comments", "");

			url = new URL(link);

			connection = (HttpURLConnection) url.openConnection();
			connection.setInstanceFollowRedirects(true);
			reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), Charset.forName(charset)));
			content = "";
			while (true) {
				String str = reader.readLine();
				if (str == null)
					break;
				content = content + str;
			}
			doc = Jsoup.parse(content);
			int index = doc.select("meta").toString().toLowerCase()
					.indexOf("charset=");
			if (index != -1) {
				charset = doc.select("meta").toString().substring(index + 8)
						.replaceAll("[^\\'\\\"0-9a-zA-Z\\-\\_]+[\\w\\W]+", "")
						.replaceAll("[\\'\\\"]+", "");
			}
			HttpURLConnection connections = (HttpURLConnection) url
					.openConnection();
			reader = new BufferedReader(new InputStreamReader(
					connections.getInputStream(), Charset.forName(charset)));

			content = "";
			while (true) {
				String str = reader.readLine();
				if (str == null)
					break;
				content = content + str;
			}
			doc = Jsoup.parse(content);
			String ti = doc.select(req.getParameter("reg_title")).text();

			String[] remove = req.getParameter("reg_remove").split(" ");

			if (remove.length > 0) {
				for (int i = 0; i < remove.length; i++) {
					try {
						if ((remove[i] != null) && (!remove[i].equals("")))
							doc.select(remove[i].replaceAll(">", " ")).remove();
					} catch (Exception localException1) {
					}
				}
			}
			String element = doc.select(req.getParameter("reg_content")).html();

			req.setAttribute("str", link);
			req.setAttribute("title", ti);
			req.setAttribute("content", Utils.cleanContent(element));
			try {
				req.getRequestDispatcher("/checkcss.jsp").forward(req, resp);
			} catch (Exception localException2) {
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}